<?php

namespace app\admin\controller\business;

use app\admin\model\basic\Goods;
use app\admin\model\business\OrderAssets;
use app\admin\model\business\StashDetail;
use app\admin\model\business\StashGoodsDetail;
use app\common\controller\Backend;

class Purchase extends Backend
{

    /**
     * Notes: 进货单录入
     * User: AerYYY
     * Date: 2/5/24 4:29 PM
     * @return string|\think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post();
            $params['admin_id'] = $this->auth->id;
            $stashDetailModel = new StashDetail();
            $stashDetailModel->startTrans();
            if ($stashDetailModel->createDetails($params)) {
                $stashDetailModel->commit();
                $this->success('ok');
            }
            $stashDetailModel->rollback();
            $this->error($stashDetailModel->getError());
        }

        $order_no = order_no('JH');
        $this->assign('order_no', $order_no);
        return $this->view->fetch();
    }



    /**
     * Notes: 进货退货单
     * User: AerYYY
     * Date: 2/5/24 11:10 PM
     * @return string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function for_goods()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post();
            $params['admin_id'] = $this->auth->id;

            $stashDetailModel = new StashDetail();
            $stashDetailModel->startTrans();
            if ($stashDetailModel->createJt($params)) {
                $stashDetailModel->commit();
                $this->success('ok');
            }
            $stashDetailModel->rollback();
            $this->error($stashDetailModel->getError());
        }

        $order_no = order_no('JT');
        $this->assign('order_no', $order_no);
        return $this->view->fetch();
    }

    /**
     * Notes: 销售进货单
     * User: AerYYY
     * Date: 2/5/24 11:22 PM
     * @return string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function xs_goods()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post();
            $params['admin_id'] = $this->auth->id;

            $stashDetailModel = new StashDetail();
            $stashDetailModel->startTrans();
            if ($stashDetailModel->createXs($params)) {
                $stashDetailModel->commit();
                $this->success('ok');
            }
            $stashDetailModel->rollback();
            $this->error($stashDetailModel->getError());
        }

        $order_no = order_no('XS');
        $this->assign('order_no', $order_no);
        return $this->view->fetch();
    }

    /**
     * Notes: 进货单修改
     * User: AerYYY
     * Date: 2/18/24 8:29 PM
     */
    public function edit_xs_goods()
    {

        if ($this->request->isPost()) {
            $params = $this->request->post();
            $params['admin_id'] = $this->auth->id;
            $stashDetailModel = new StashDetail();
            $stashDetailModel->startTrans();
            if ($stashDetailModel->edit_xs_goods($params)) {
                $stashDetailModel->commit();
                $this->success('ok');
            }
            $stashDetailModel->rollback();
            $this->error($stashDetailModel->getError());
        }
        $order_no = input('order_no');
        if (empty($order_no)) {
            $this->error('错误的参数');
        }
        $order = StashDetail::where('order_no', $order_no)->find();
        if (empty($order)) {
            $this->error('错误的订单');
        }
        $list = StashGoodsDetail::where('order_no', $order_no)->select();
        $order['sum_qty'] = 0;
        foreach ($list as &$value) {
            $order['sum_qty'] += $value['qty'];
            $value['goods'] = Goods::where('id', $value['goods_id'])->find();
        }
        $new_key = count($list) + 1;
        $this->assign('new_key', $new_key);
        $this->assign('order', $order);
        $this->assign('list', $list);
        return $this->view->fetch();

    }
    /**
     * Notes: 销售退货单
     * User: AerYYY
     * Date: 2/6/24 4:53 PM
     * @return string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function xt_goods()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post();
            $params['admin_id'] = $this->auth->id;

            $stashDetailModel = new StashDetail();
            $stashDetailModel->startTrans();
            if ($stashDetailModel->createXt($params)) {
                $stashDetailModel->commit();
                $this->success('ok');
            }
            $stashDetailModel->rollback();
            $this->error($stashDetailModel->getError());
        }

        $order_no = order_no('XT');
        $this->assign('order_no', $order_no);
        return $this->view->fetch();
    }

    /**
     * Notes: 收款单
     * User: AerYYY
     * Date: 2/6/24 7:48 PM
     * @return string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function for_payment()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post();
            $params['admin_id'] = $this->auth->id;

            $orderAssetsModel = new OrderAssets();
            $orderAssetsModel->startTrans();
            if ($orderAssetsModel->for_payment($params)) {
                $orderAssetsModel->commit();
                $this->success('ok');
            }
            $orderAssetsModel->rollback();
            $this->error($orderAssetsModel->getError());
        }

        $order_no = order_no('SK');
        $this->assign('order_no', $order_no);
        return $this->view->fetch();
    }

    /**
     * Notes: 付款单
     * User: AerYYY
     * Date: 2/8/24 4:08 PM
     * @return string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function bill_payment()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post();
            $params['admin_id'] = $this->auth->id;

            $orderAssetsModel = new OrderAssets();
            $orderAssetsModel->startTrans();
            if ($orderAssetsModel->bill_payment($params)) {
                $orderAssetsModel->commit();
                $this->success('ok');
            }
            $orderAssetsModel->rollback();
            $this->error($orderAssetsModel->getError());
        }

        $order_no = order_no('FK');
        $this->assign('order_no', $order_no);
        return $this->view->fetch();
    }

    /**
     * Notes: 转账单
     * User: AerYYY
     * Date: 2/17/24 6:10 PM
     * @return string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function transfer_payment()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post();
            $params['admin_id'] = $this->auth->id;

            $orderAssetsModel = new OrderAssets();
            $orderAssetsModel->startTrans();
            if ($orderAssetsModel->transfer_payment($params)) {
                $orderAssetsModel->commit();
                $this->success('ok');
            }
            $orderAssetsModel->rollback();
            $this->error($orderAssetsModel->getError());
        }

        $order_no = order_no('TCZ');
        $this->assign('order_no', $order_no);
        return $this->view->fetch();

    }
}